//
// Created by dxyinme on 2021/11/14.
//

#ifndef FPSTYLE_LOOP_H
#define FPSTYLE_LOOP_H

#include "task.h"

#include <functional>
#include <cstdlib>
#include <algorithm>

namespace FPStyle {

enum class Control {
  BREAK,
  CONTINUE,
};

template <class F, class IndexType>
IndexType ForIndex(IndexType bg, IndexType ed, F&& f, IndexType Increment = 1) {
  while (bg < ed) {
    Task<Control> t;
    f(bg, t);
    if (t.ret == Control::BREAK) {
      break;
    }
    bg += Increment;
  }
  return bg;
}

template <class F, class ValueType, class BreakCaseFuncType, class NextValueFuncType>
ValueType ForCase(ValueType v_bg,
        BreakCaseFuncType&& breakCase,
        NextValueFuncType&& nextValueFunc,
        F&& f) {
  while (!breakCase(v_bg)) {
    Control c = f(v_bg);
    if (c == Control::BREAK) {
      break;
    }
    v_bg = nextValueFunc(v_bg);
  }
  return v_bg;
}

}

#endif //FPSTYLE_LOOP_H

